clear
use "Resource Cure Data"
sort ccode year
xtset ccode year
***********************************
**************TABLE 1**************
***********************************

*MODEL 1*
eststo clear
xi: eststo: reg FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year, cl(ccode)
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

*MODEL 2*
xi: eststo: xtreg FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year,fe cl(ccode)
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

*MODEL 3*
xi: eststo: xtabond2 FH l.FH lenerdpl  L.(lenerdpl ) i.year if CP!=., gmm(l.FH,c) iv( lenerdpl L.(lenerdpl) i.year) small robust two
di "Energy Rents When, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 4)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
*

*MODEL 4*
xi: eststo: xtabond2 FH l.FH CP lenerdpl  L.(CP lenerdpl ) i.year, gmm(l.FH,c) iv( CP lenerdpl L.(CP lenerdpl) i.year) small robust two
di "Energy Rents When, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[CP]
lincom _b[L.CP]
di "Energy Rents When, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
lincom _b[CP]
lincom _b[L.CP]
di "Energy Rents When, Long-term Effect, GMM, `i' period (Model 4)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 4)"
nlcom (_b[CP]+_b[L.CP])/(1-_b[l.FH])
*MODEL 5*
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N sarganp hansenp ar1p ar2p j N_g) r2  varwidth(25) modelwidth(6)       ///
title(Table. )       ///
nonumbers mtitles("Model 1" "Model 2" " Model 3"  "Model 4" " Model 5")  ///
drop(*year*) ///
addnote("Year dummies are not presented to save space") ///
nogap 

***********************************
**************TABLE 2**************
***********************************
eststo clear
global controls income topenness population
foreach v in  $controls {
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP `v' L.(CP lenerdpl lenerdplCP `v') i.year, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP `v' L.(CP lenerdpl lenerdplCP `v') i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

di "LONG AND SHORT RUN EFFECTS OF `v'"
di ""
lincom _b[`v']
lincom _b[L.`v']
nlcom (_b[`v']+_b[L.`v'])/abs(1-_b[l.FH])
}
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP muslim L.(CP lenerdpl lenerdplCP muslim) i.year, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP muslim L.(CP lenerdpl lenerdplCP muslim) i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])
lincom _b[muslim]
*lincom _b[L.muslim]
nlcom (_b[muslim])/abs(1-_b[l.FH])

global controls  kshare2_0100 kshare2sq_0100
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP $controls L.(CP lenerdpl lenerdplCP $controls) i.year, gmm(l.FH,c) iv(CP lenerdpl lenerdplCP $controls L.(CP lenerdpl lenerdplCP $controls) i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

nlcom (_b[kshare2_0100]+_b[L.kshare2_0100])/abs(1-_b[l.FH])
nlcom (_b[kshare2sq_0100]+_b[L.kshare2sq_0100])/abs(1-_b[l.FH])


global controls income topenness population muslim kshare2_0100 kshare2sq_0100
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP $controls L.(CP lenerdpl lenerdplCP $controls) i.year, gmm(l.FH,c) iv(CP lenerdpl lenerdplCP $controls L.(CP lenerdpl lenerdplCP $controls) i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])

foreach v in income topenness population kshare2_0100 kshare2sq_0100 {
nlcom (_b[`v']+_b[L.`v'])/abs(1-_b[l.FH])
}
nlcom (_b[muslim])/abs(1-_b[l.FH])

esttab, b(a2) se(2) replace label star(* 0.10 ** 0.05 *** 0.01) scalars(N sarganp hansenp ar1p ar2p j N_g) r2  varwidth(25) modelwidth(6)       ///
title(Table. )       ///
nonumbers mtitles("Model 1" "Model 2" " Model 3"  "Model 4" " Model 5" " Model 6")  ///
drop(*year*) ///
addnote("Year dummies are not presented to save space") ///
nogap 

su FH lenerdpl CP lenerdplCP income topenness population muslim

**************************************************************
***********FOR FIGURE 1*******************************************
**************************************************************
*MODEL 5, TABLE 1*
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year) small robust two
di "Energy Rents When CP=0, Short-term Effect, GMM, `i' period (Model 5)" 
lincom _b[lenerdpl]
lincom _b[L.lenerdpl]
di "Energy Rents When CP=1, Short-term Effect, GMM, `i' period (Model 5)"
lincom _b[lenerdpl]+_b[lenerdplCP]
lincom _b[L.lenerdpl]+_b[L.lenerdplCP]  
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])
*Energy Rents for Contract rich countries do not have any effect on democracy,
*whereas Energy Rents for Contract poor countries have long-term effects.

preserve
{
gen period0=-.2040636
gen period1=-.2609957+(_b[l.FH]*period0)
forvalues i=2(1)19 {
local k=`i'-1
gen period`i'=_b[l.FH]*period`k'
}
keep period*
keep if _n == 1
gen ID=1
reshape long period, i(ID) j(time)
generate runsum = sum(period)
gen cumulativeperc=runsum/-9.332794
save "Graph 1.dta", replace
}
restore

/*
FIGURE 1 CAN BE RETRIEVED WITH THE FOLLOWING R-SCRIPT*
library(foreign)
mydata <- read.dta("Graph 1.dta")
summary(mydata)
dim(mydata)
names(mydata)
library(ggplot2)
ggplot(data=mydata, aes(x=time, y=runsum))+ 
theme(axis.title.x = element_text(colour="black", size=8))+
theme(axis.title.y = element_text(colour="black", size=8))+
xlab("Time Periods") +  ylab(expression(paste(Delta, Democracy)))+
geom_bar(colour="black",stat="identity", position=position_dodge(), width=.6, fill="gray")+
ggtitle("Distribution of the Total Effect over Time")+ 
theme_bw()+theme_classic(base_size = 10, base_family = "")*/

*********************************************************************
***********APPENDIX**************************************************
*********************************************************************
qui xtabond2 FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year) small robust two

xtunitroot fisher FH if e(sample), dfuller lags(1)

*POST-1980 YEARS FOR MODEL 5
eststo clear
xi: eststo: xtabond2 FH l.FH CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year if year>1979, gmm(l.FH,c) iv( CP lenerdpl lenerdplCP L.(CP lenerdpl lenerdplCP) i.year) small robust two
di "Energy Rents When CP=0, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[L.lenerdpl])/(1-_b[l.FH])
di "Energy Rents When CP=1, Long-term Effect, GMM, `i' period (Model 5)"
nlcom (_b[lenerdpl]+_b[lenerdplCP]+_b[L.lenerdpl]+_b[L.lenerdplCP])/abs(1-_b[l.FH])
